﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using Vbyte.SharpOrm.Config;
using Vbyte.SharpOrm;

namespace NUnitTest
{
    #if NETFX35
    /// <summary>
    /// 产品/业务实例
    /// </summary>
    [Serializable]
    [BindTable("DefaultDB", "gw_Publish_Product")]
    public class Product : TableEntry, IEntryExtension<Product>
    {
        #region 实体映射
        [PrimaryKey]
        public long ProductID { get; set; }
        /// <summary>
        /// 获取或设置产品名称
        /// </summary>
        /// <value>The name of the product.</value>
        public string ProductName { get; set; }

        /// <summary>
        /// 产品分类编号
        /// </summary>
        /// <value>The PCID.</value>
        [TableItem("PC_ID")]
        public short PCID { get; set; }

        /// <summary>
        /// 产品编号
        /// </summary>
        [TableItem("P_Code")]
        public string PCode { get; set; }

        /// <summary>
        /// 运营商编号
        /// </summary>
        public int ISP_ID { get; set; }

        /// <summary>
        /// 运营商业务代码
        /// </summary>
        public string ISP_Code { get; set; }

        /// <summary>
        /// 产品状态编号
        /// </summary>
        public int PS_ID { get; set; }

        /// <summary>
        /// SP编号
        /// </summary>
        public int Sp_id { get; set; }

        #region 暂不使用
        ///// <summary>
        ///// 计费模式编号
        ///// </summary>
        //public string AT_ID { get; set; }

        ///// <summary>
        ///// 计费URL
        ///// </summary>
        //public string AT_URL { get; set; }

        ///// <summary>
        ///// 免费URL
        ///// </summary>
        //public string Free_URL { get; set; }

        ///// <summary>
        ///// 入口URL
        ///// </summary>
        //public string Enter_URL { get; set; }

        ///// <summary>
        ///// 确认URL
        ///// </summary>
        //public string Submit_URL { get; set; } 
        #endregion

        /// <summary>
        /// 产品备注
        /// </summary>
        public string P_Info { get; set; }

        //public bool P_AuditTF { get; set; }

        /// <summary>
        /// 添加时间
        /// </summary>
        public DateTime P_AddDate { get; set; }
        #endregion


        #region IEntryExtension<Product> 成员 [发布目录扩展]

        public Product SetWithExtension(DictionaryEntry[] entryArray)
        {
            string PubPath = "";
            //发布目录及平台扩展
            Product p = new Product().DataBind<Product>(entryArray,
                delegate(string propName, object propValue)
                {
                    if (propName == "PubPath") PubPath = propValue.ToString();
                });

            if (p.ProductID == 0)
            {
                p.ProductID = Convert.ToInt64(p.Insert(true));
            }
            else
            {
                p.Update();
            }

            if (PubPath.Trim() != string.Empty)
            {
                //PublishProductEvment ppe = new PublishProductEvment() { ProductId = p.ProductID, PE_Id = 5 };
                //PublishProductEvment nPPE = new PublishProductEvment()
                //{
                //    ProductId = p.ProductID, PublicPath = PubPath, PE_Id = 5
                //}; 

                //using (DbConnection sharedOpenConn = ppe.GetDbConnection())
                //{
                //    sharedOpenConn.Open();
                //    if (ppe.GetTotalRecord<PublishProductEvment>(sharedOpenConn) < 1)
                //    {
                //        nPPE.Insert(sharedOpenConn);
                //    }
                //    else
                //    {
                //        ppe.ReplaceWith<PublishProductEvment>(nPPE, sharedOpenConn);
                //    }
                //    sharedOpenConn.Close();
                //}
            }
            return p;
        }

        public DictionaryEntry[] GetExtenEntryArray()
        {
            List<DictionaryEntry> entryArray = new List<DictionaryEntry>();
            //PublishProductEvment[] ppeList = new PublishProductEvment() { ProductId = this.ProductID  }
            //    .GetDataList<PublishProductEvment>(new string[] { "PublicPath" });
            //List<string> ppList = new List<string>(ppeList.Length);
            //foreach (PublishProductEvment ppe in ppeList)
            //{
            //    ppList.Add(ppe.PublicPath);
            //}
            //entryArray.Add(new DictionaryEntry("PubPath", string.Join("|", ppList.ToArray())));
            return entryArray.ToArray();
        }


        public void RemoveExtension()
        {
            //new PublishProductEvment() { ProductId = this.ProductID }.Delete();
        }

        #endregion
    }
    #endif
}